def primex (n):
    for i in range(2,int(n**0.5)+1):
        if n%i==0:
            return False
    return True

n=int(input())

n1=n//1000
n2=(n//100)%10
n3=(n//10)%10
n4=n%10

primem=0

if primex(n1) and primem<n1: primem=n1
if primex(n2) and primem<n2: primem=n2
if primex(n3) and primem<n3: primem=n3
if primex(n4) and primem<n4: primem=n4


n12=n1*10+n2
n13=n1*10+n3
n14=n1*10+n4
n23=n2*10+n3
n24=n2*10+n4
n34=n3*10+n4

if primex(n12) and primem<n12: primem=n12
if primex(n13) and primem<n13: primem=n13
if primex(n14) and primem<n14: primem=n14
if primex(n23) and primem<n23: primem=n23
if primex(n24) and primem<n24: primem=n24
if primex(n34) and primem<n34: primem=n34

n123=n1*100+n2*10+n3
n124=n1*100+n2*10+n4
n234=n2*100+n3*10+n4
n134=n1*100+n3*10+n4

if primex(n123) and primem<n123: primem=n123
if primex(n124) and primem<n124: primem=n124
if primex(n234) and primem<n234: primem=n234
if primex(n134) and primem<n134: primem=n134

if primex(n) and primem<n: primem=n

if n >999 and n<10000:
    print(primem)
else: print('0')






